
package com.android.fitnesscenter.view.server;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.fitnesscenter.FitnessCenterContants.ResultCode;
import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ServerGetPasswordActivity extends BaseActivity {

    @ViewInject(R.id.btn_next)
    private Button btn_next;

    @ViewInject(R.id.txt_question_title_1)
    private TextView txt_question_title_1;

    @ViewInject(R.id.edtxt_question_1)
    private EditText edtxt_question_1;

    @ViewInject(R.id.txt_question_title_2)
    private TextView txt_question_title_2;

    @ViewInject(R.id.edtxt_question_2)
    private EditText edtxt_question_2;

    @ViewInject(R.id.txt_question_title_3)
    private TextView txt_question_title_3;

    @ViewInject(R.id.edtxt_question_3)
    private EditText edtxt_question_3;

    private String username, question1, question2, question3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.get_password_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_get_password));
        Intent intent = getIntent();
        if (intent != null) {
            username = intent.getStringExtra("username");
            question1 = intent.getStringExtra("question1");
            question2 = intent.getStringExtra("question2");
            question3 = intent.getStringExtra("question3");
            txt_question_title_1.setText("1." + question1);
            txt_question_title_2.setText("2." + question2);
            txt_question_title_3.setText("3." + question3);
        }
    }

    @OnClick(R.id.btn_next)
    public void btn_next(View view) {
        String answer1 = "";
        if (edtxt_question_1 != null && edtxt_question_1.getText() != null) {
            answer1 = edtxt_question_1.getText().toString();
        }
        String answer2 = "";
        if (edtxt_question_2 != null && edtxt_question_2.getText() != null) {
            answer2 = edtxt_question_2.getText().toString();
        }
        String answer3 = "";
        if (edtxt_question_3 != null && edtxt_question_3.getText() != null) {
            answer3 = edtxt_question_3.getText().toString();
        }
        if (TextUtils.isEmpty(answer1) || TextUtils.isEmpty(answer2) || TextUtils.isEmpty(answer3)) {
            ToastHelper.getInstance().showShortMsg("答案为空,请输入答案!");
            return;
        }
        findPassword(answer1, answer2, answer3);
    }

    private void findPassword(String answer1, String answer2, String answer3) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("username", username);
        params.addBodyParameter("question1", question1);
        params.addBodyParameter("question2", question2);
        params.addBodyParameter("question3", question3);
        params.addBodyParameter("answer1", answer1);
        params.addBodyParameter("answer2", answer2);
        params.addBodyParameter("answer3", answer3);

        String url = ConfigProperties.SERVICE_URL + "findPassword.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(ServerGetPasswordActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ServerGetPasswordActivity.this != null && !ServerGetPasswordActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        try {
                            String result = stringResponseInfo.result;
                            Loger.e("result is " + result);
                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    String secret = jsonObject.getString("secret");
                                    Intent intent = new Intent(ServerGetPasswordActivity.this,
                                            ServerNewPasswordActivity.class);
                                    intent.putExtra("secret", secret);
                                    intent.putExtra("username", username);
                                    startActivityForResult(intent, 0x0001);
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(ServerGetPasswordActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ServerGetPasswordActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                if (ServerGetPasswordActivity.this != null && !ServerGetPasswordActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ServerGetPasswordActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ResultCode.GET_PASSWORD) {
            setResult(ResultCode.GET_PASSWORD);
            finish();
        }
    }

    @Override
    protected void titlebarMethod(int id) {

    }

}
